#CSV reader

log_file = open('log_file.csv','r')
header_length = len(log_file.readline())
log_file.close()
log_file = open('log_file.csv','r')
log_txt = log_file.read()
#print log_txt
rpm_tag = 'r'
GPS_tag = 'g'
vel_tag = 'v'
acc_tag = 'a'
batt_tag = 'b'
gear_tag = 'e'


tags = []
tags.append(gear_tag)
tags.append(vel_tag)
tags.append(rpm_tag)
tags.append(acc_tag)

#parse header
m_header = []

for i in range(0,len(tags)):
    #print i
    m_header.append([])
    sample_rate = 0
    for j in range(0,64):
        if log_txt[j] == tags[i]:
            j=j+2
            for k in range(1,8):
                if log_txt[j+k]==',' or log_txt[j+k]=='\n':
                    sample_rate = log_txt[j:j+k]
                    #print sample_rate
                    break
            break
    m_header[i].append((tags[i],sample_rate))


#parse data, note:gps requires a seperate function
def read_data_points(tag, header_length):
    data_points = []
    time_stamp_points = []
    if tag == GPS_tag:
        data_points, time_stamp_points = read_GPS_points(header_length)
    else:
        for i in range(header_length,len(log_txt)-3):
            if log_txt[i]==tag:
                i = i+2
                j=1
                while (log_txt[i+j] != ',') and (log_txt[i+j] != '\n'):
                    #print log_txt[i+j]
                    j = j+1
                time_stamp = log_txt[i:i+j]
                #print "time+stamp=", time_stamp
                time_stamp_points.append(time_stamp)
                i=i+j+1
                j=1
                while log_txt[i+j] != ',' and log_txt[i+j] != '\n':
                    j = j+1
                data = log_txt[i:i+j]
                i=i+j+1
                data_points.append(data)
    return data_points, time_stamp_points
#read GPS points; data_points=2D lat,long;time_points=UTC Time
def read_GPS_points(header_length):
    data_points = []
    time_points = []

    return data_points, time_points

vel_points,vel_times = read_data_points(vel_tag,header_length)
GPS_points,GPS_times = read_data_points(GPS_tag,header_length)
rpm_points, rpm_times = read_data_points(rpm_tag,header_length)
gear_points, gear_times = read_data_points(gear_tag,header_length)
batt_points, batt_times = read_data_points(batt_tag,header_length)
acc_points, acc_times = read_data_points(acc_tag,header_length)
#print acc_points

log_file.close()